package jp.co.itfrontier.mashup.command
{
	import jp.co.itfrontier.mashup.event.IEvent;
	import jp.co.itfrontier.mashup.core.ModelLocater;
	import jp.co.itfrontier.mashup.vo.BookVO;
	import mx.logging.Log;
	import flash.net.getClassByAlias;
	import jp.co.itfrontier.mashup.core.Logger;
	import flash.net.URLVariables;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.events.Event;
	import jp.co.itfrontier.mashup.utils.CrossDomainUtil;
	import jp.co.itfrontier.mashup.event.ChangeModelEvent;
	import jp.co.itfrontier.mashup.core.EventController;
	
	public class KizasiSearch{

		// URL			
		private const Kizasi_url:String = "http://kizasi.jp/kizapi.py";
		//1ヶ月
		private const Kizasi_span:String = "1m";
		private const Kizasi_type:String = "coll"

		// データを取得するURLを保持するクラス
		private var urlRequest:URLRequest;

			// 検索キーワード
		private var keyword:String;


		public function getSentense(key:String):void {

			keyword = key;

			//探してみます						
			var urlVar1:URLVariables = new URLVariables();
			urlVar1.span = Kizasi_span;
			urlVar1.type = Kizasi_type;
			urlVar1.kw_expr = encodeURI(keyword);
			
			urlRequest = new URLRequest();
			urlRequest.url = Kizasi_url;
			urlRequest.data = urlVar1;
 
 			var proxy:CrossDomainUtil = new CrossDomainUtil();
 			proxy.requestProxy(urlRequest, responseCompleteHandler);
 
			Logger.getInstance().info("kizasi::execute");
		}
		

		/**
		 * kizasi関連語取得処理のCompleteハンドラ
		 * 
		 */		
		private function responseCompleteHandler(event:Event):void {

			var urlLoader:URLLoader = event.target as URLLoader;
			
			var resultXML:XML = new XML(urlLoader.data);
			
			var kizasiArray:Array = new Array();
			
			for each(var xml:XML in resultXML..item) {
				kizasiArray.push(String(xml.title));
			}
			
			ModelLocater.getInstance().kizasiWords = kizasiArray;

			Logger.getInstance().info("kizasi:" + kizasiArray.join(" "));

			EventController.getInstance().dispatchEvent(new ChangeModelEvent(ChangeModelEvent.KIZASI));


		}

	}
}